﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using TarreOA.TarreOAModels;
using TarreOA.TarreOABLL;

public partial class admin_AddSellInfo : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["user"] == null)
        {
            Session["ShowMessage"] = "用户未登录，请重新登录!";
            Session["Backurl"] = "Login.aspx";
            Response.Redirect("~/Main.aspx");
        }
        else
        {
            TarreOA.TarreOAModels.User sucessUser = (User)Session["user"];
            bool userFlag = false;
            if (sucessUser.PopedomGroup.PopedomList != null)
            {
                foreach (Popedom var in sucessUser.PopedomGroup.PopedomList)
                {
                    if (var.PopedomId == 42)
                    {
                        userFlag = true;
                        break;
                    }
                }
            }
            if (!userFlag)
            {
                Session["ShowMessage"] = "用户没有该权限，请与管理员联系!";
                Session["Backurl"] = "Right.aspx";
                Response.Redirect("~/Message.aspx");
            }
        }
        if (!IsPostBack)
        {
            TarreOA.TarreOAModels.User user = (User)Session["user"];
            if (user.ShellManager == 1)
            {
                Label1.Text = "新建销售记录（销售记录总数 " + SellInfoManager.CountAllSellInfo() + " ）";
            }
            else
            {
                Label1.Text = "新建销售记录（销售记录总数 " + SellInfoManager.CountAllSellInfo(user.UserId) + " ）";
            }
            this.RangeValidator2.MaximumValue = decimal.MaxValue.ToString();
            this.RangeValidator3.MaximumValue = int.MaxValue.ToString();
        }
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        SellInfo sellInfo = new SellInfo();
        if (hidClientId.Value != "")
        {
            Client client = new Client();
            client.ClientId = Convert.ToInt32(hidClientId.Value);
            sellInfo.Client = client;
        }
        if (hidProductId.Value != "")
        {
            Product product = new Product();
            product.Productid = Convert.ToInt32(hidProductId.Value);
            sellInfo.Product = product;
        }
        if (txtSellDate.Text != null && txtSellDate.Text != "")
        {
            sellInfo.SellDate = Convert.ToDateTime(txtSellDate.Text.Trim());
        }
        else
        {
            sellInfo.SellDate = System.DateTime.Now;
        }
        if (txtCount.Text != null && txtCount.Text != "")
        {
            sellInfo.Count = Convert.ToInt32(txtCount.Text.Trim());
        }
        else
        {
            sellInfo.Count = 0;
        }
        if (txtPrice.Text != null && txtPrice.Text != "")
        {
            sellInfo.Price = Convert.ToDecimal(txtPrice.Text.Trim());
        }
        else
        {
            sellInfo.Price=0;
        }
        if (txtAgio.Text != null && txtAgio.Text != "")
        {
            sellInfo.Agio = Convert.ToInt32(txtAgio.Text.Trim());
        }
        else
        {
            sellInfo.Agio = 0;
        }
        if (txtConsignmentUser.Text != null && txtConsignmentUser.Text != "")
        {
            sellInfo.ConsignmentUser = txtConsignmentUser.Text.Trim();
        }
        else 
        {
            sellInfo.ConsignmentUser = "";
        }
        if (txtRemark.Text != null && txtRemark.Text != "")
        {
            sellInfo.Remark = txtRemark.Text.Trim();
        }
        else
        {
            sellInfo.Remark = "";
        }
        sellInfo.ShellUser=(User)Session["user"];
        if (SellInfoManager.AddSellInfo(sellInfo))
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "alert('添加成功！')", true);
        }
        else
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "alert('添加失败！请重试！')", true);
        }
    }
}
